package org.easy.module.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.easy.common.constant.TipConstant;
import org.easy.common.domain.SysDictDataDO;
import org.easy.common.restinfo.RestInfo;
import org.easy.common.vo.SysDictDataVO;
import org.easy.module.mapper.SysDictDataMapper;
import org.easy.module.mapper.SysDivisionsMapper;
import org.easy.module.service.SysDictDataService;
import org.easy.module.service.SysDictService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: SysDictDataServiceImpl
 * @Description: 数据字典-数据实现类
 * @Author zhouzhixin
 * @Date 2021/10/9
 * @Version 1.0
 */
@Slf4j
@Service
public class SysDictDataServiceImpl extends ServiceImpl<SysDictDataMapper, SysDictDataDO> implements SysDictDataService {

    SysDictDataMapper sysDictDataMapper;
    SysDictService sysDictService;
    SysDivisionsMapper sysDivisionsMapper;

    @Autowired
    public void init(SysDictDataMapper sysDictDataMapper, SysDictService sysDictService, SysDivisionsMapper sysDivisionsMapper) {
        this.sysDictDataMapper = sysDictDataMapper;
        this.sysDictService = sysDictService;
        this.sysDivisionsMapper = sysDivisionsMapper;
    }

    /**
     * <p>列表查询</p>
     *
     * @param
     * @return java.util.List<org.easy.common.vo.SysDictDataVO>
     * @author zhouzhixin
     * @date 2021/10/29 14:17
     */
    @Override
    public List<SysDictDataVO> queryList() {
        List<SysDictDataVO> dictDataVOList = sysDictDataMapper.queryList();
        // 非空判断，为空则返回空集合
        if (CollectionUtils.isEmpty(dictDataVOList)) {
            dictDataVOList = new ArrayList<>();
        }
        return dictDataVOList;
    }

    @Override
    public RestInfo<Void> update(SysDictDataVO sysDictDataVO) {
        SysDictDataDO sysDictDataDO = new SysDictDataDO();
        BeanUtils.copyProperties(sysDictDataVO, sysDictDataDO);
        boolean updateRes = updateById(sysDictDataDO);
        if (updateRes) {
            return RestInfo.ok().build();
        }
        return RestInfo.fail().build();
    }

    /**
     * <p>验证字典数据是否存在</p>
     *
     * @param sysDictDataDO
     * @return java.lang.String
     * @author zhouzhixin
     * @date 2021/10/11 16:40
     */
    @Override
    public String checkData(SysDictDataDO sysDictDataDO) {
        List<SysDictDataDO> dictDataDOList;
        dictDataDOList = sysDictDataMapper.selectList(new QueryWrapper<SysDictDataDO>().lambda()
                .eq(SysDictDataDO::getDataName, sysDictDataDO.getDataName())
                .eq(SysDictDataDO::getDataValue, sysDictDataDO.getDataValue()));
        if (!CollectionUtils.isEmpty(dictDataDOList)) {
            return TipConstant.CHECK_DICT_DATA_EXIST_DATA;
        }
        return null;
    }

}
